package com.config;

import com.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyCorsConfiguration {
    @Autowired
    private LoginInterceptor loginInterceptor;

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            //统一跨域配置
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")//允许哪些IP地址跨域访问
                        .allowedMethods("*")//运行跨域访问的method类型,post get put delete
                        .allowedHeaders("*");//跨域访问允许前端携带请求头，后面登录会使用
            }

            //拦截器配置
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(loginInterceptor)
                        .addPathPatterns("/**")//设置要拦截的路径
                        .excludePathPatterns(
                                "/employee/login",
                                "/verifyCode/**");//设置特殊请求路径放行
            }
        };
    }
}
